/**
 * 
 */
package com.desksoft.ecommerce.helper;

import static com.google.common.base.Preconditions.checkNotNull;
import net.sf.ehcache.CacheManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.desksoft.ecommerce.util.holder.ApplicationContextHolder;
import com.desksoft.utility.cache.ehcache.CacheWrapper;
import com.desksoft.utility.cache.ehcache.EhcacheWrapper;

/***************************************
 * <pre>
 * 版权所有：杭州迪科软件 保留所有权利
 * 创建日期: Nov 16, 2011 3:28:48 PM
 * 创建作者：yejw
 * 文件名称：EHCacheHelper.java
 * 版本：1.0
 * 功能：
 * 最后修改时间：Nov 16, 2011 3:28:48 PM
 * 修改记录：
 * </pre>
 *****************************************/
public class EHCacheHelper {
	
	protected final static Log log = LogFactory.getLog(EHCacheHelper.class);
	
	private static CacheManager defaultCacheManager = null;
	
	static {
		defaultCacheManager = (CacheManager) ApplicationContextHolder.getBean("cacheManager");
	}
	
	/**
	 * 
	 * @param <K>
	 * @param <V>
	 * @return
	 */
	public static <K, V> CacheWrapper<K, V> getEHCache() {
		return getEHCache("desksoft_cache");
	}
	
	/**
	 * 
	 * @param <K>
	 * @param <V>
	 * @return
	 */
	public static <K, V> CacheWrapper<K, V> getOneDayEHCache() {
		return getEHCache("desksoft_one_day_cache");
	}
	
	/**
	 * 
	 * @param <K>
	 * @param <V>
	 * @param cacheName
	 * @return
	 */
	public static <K, V> CacheWrapper<K, V> getEHCache(String cacheName) {
		checkNotNull(cacheName);
		checkNotNull(defaultCacheManager);
		
		return new EhcacheWrapper<K, V>(cacheName, defaultCacheManager);
	}

	/**
	 * 
	 * @param <K>
	 * @param <V>
	 * @param cacheName
	 * @param cacheManagerName
	 * @return
	 */
	public static <K, V> CacheWrapper<K, V> getEHCache(String cacheName, String cacheManagerName) {
		checkNotNull(cacheName);
		
		CacheManager cacheManager = (CacheManager) ApplicationContextHolder.getBean(cacheManagerName);
		checkNotNull(cacheManager);
		
		return new EhcacheWrapper<K, V>(cacheName, cacheManager);
	}
}
